<?php
namespace app\common\controller;

use app\common\model\admin\AccountModel;
use app\common\model\jiyun\base\ChannelModel;
use app\common\model\jiyun\warehouse\WarehouseAbroadModel;
use app\common\model\jiyun\warehouse\WarehouseModel;
use app\common\model\shop\CurrencyModel;
use app\common\model\shop\ShopsModel;
use think\admin\Controller;
use think\admin\model\SystemUser;

class Ajax extends Controller
{
    /**
     * 获取老师列表
     *
     * @Description
     * @author wangqinghui
     * @date 2024-11-04 16:57
     * @return void
     */
    public function getTeacherList ()
    {
        $account_id = $this->request->get('account_id');

        $data = SystemUser::where('account_id', $account_id)->where('usertype', 'teacher')->where('is_deleted' , 0)->column('nickname', 'id');

        $this->success(lang('成功'), $data);

    }

    /**
     * 获取用户身份
     *
     * @Description
     * @author wangqinghui
     * @date 2024-11-04 16:57
     * @return void
     */
    public function getUserIdentity()
    {
        $account_id = $this->request->get('account_id');

        $identity = AccountModel::mk()->where('id', $account_id)->whereRaw("find_in_set('".AccountModel::MODULE_SCHOOLS."', module)")->count();

        $this->success(lang('成功'), ['identity' => $identity]);
    }

    /**
     * 获取非实战用户
     *
     * @Description
     * @author wangqinghui
     * @date 2024-11-04 16:57
     * @return void
     */
    public function getShopUserList ()
    {
        $account_id = $this->request->get('account_id');

        $data = SystemUser::where('account_id', $account_id)->whereIn('usertype', SystemUser::getNoTeacherUserType())->where('is_deleted' , 0)->column('nickname', 'id');

        $this->success(lang('成功'), $data);
    }
    /**
     * 获取币种列表
     *
     * @Description
     * @author wangqinghui
     * @date 2024-11-04 16:57
     * @return void
     */
    public function getCurrencyList() 
    {
        $map = [
            ['status',  '=',    1]
        ];
        $field = 'id,currency_name,currency_name_en,abbreviation,symbol,exchange_rate,is_default';
        $list = CurrencyModel::mk()->field($field)->where($map)->select()->toArray();
        $this->success(lang('获取成功'), $list);
    }

    /**
     * 获取授权店铺
     * @Description
     * @author wangqinghui
     * @date 2024-11-21 14:18
     * @return void
     */
    public function getShopList()
    {
        $account_id  = (int) $this->request->post('account_id', 0);
        $platform_id = (int) $this->request->post('platform_id', 0);
        if(empty($account_id) || ($account_id <= 0)){
            $this->error(lang('请选择公司'));
        }
        if(empty($platform_id) || ($platform_id <= 0)){
            $this->error(lang('请选择平台'));
        }
        // 获取店铺列表
        $where = [
            ['platform_id', '=', $platform_id],
            ['account_id', '=', $account_id],
            ['status', '=', 1],
            ['is_deleted', '=', 0]
        ];
        $shop_list = ShopsModel::mk()->where($where)->column('id,name');
        $this->success(lang('获取成功'), $shop_list);
    }

    /**
     * 获取店铺信息
     * @Description
     * @author wangqinghui
     * @date 2024-11-25 18:09
     * @return void
     */
    public function getShopInfo()
    {
        $shop_id = (int)$this->request->post('shop_id', 0);
        $currency_flag = $this->request->post('currency_flag', 0);
        if(empty($shop_id) || ($shop_id <= 0)){
            $this->error(lang('请选择店铺'));
        }
        $field = 'id,name,client_id,currency_id,account_id';
        $map = [
            ['id', '=', $shop_id],
        ];
        $shop_info = ShopsModel::mk()->field($field)->where($map)->find();
        $shop_data = !empty($shop_info) ? $shop_info->toArray() : [];
        if(!empty($shop_data)){
            if(!empty($currency_flag)){
                $currency_info = CurrencyModel::mk()->field('id,currency_name,currency_name_en,abbreviation,symbol,exchange_rate')->find($shop_data['currency_id']);
                $shop_data['currency_info'] = !empty($currency_info) ? $currency_info->toArray() : [];
            }
        }
        $this->success(lang('获取成功'), $shop_data);
    }

    /**
     * 根据公司获得渠道列表
     *
     * @author shirukunn
     * @edit  Lihongqi - 2024/11/30 17:25
     * @date 2024/11/26 17:25
     */
    public function getChannels()
    {
        $account_id = $this->request->param('account_id', 0);
//        halt($account_id);
        $channels = ChannelModel::mk()->where(function ($query) use ($account_id) {
            if ($account_id) {
                $query->where('account_id', $account_id);
            }
        })->where('status', 1)->select()->toArray();

        $this->success(lang('获取成功'), $channels);
    }

    /**
     * 根据仓库获得渠道
     *
     * @return void
     */
    public function getChannelByWarehouse()
    {
        $warehouse_id = $this->request->get('warehouse_id');
        $channel_ids  = WarehouseModel::mk()->where('id', $warehouse_id)->value('channel_ids');

        $channel_ids_arr  = explode(',', $channel_ids);

        $channel_obj= ChannelModel::mk()->with('country')->whereIn('id',$channel_ids_arr)->select();

        $channel_arr = [];
        $channel_obj->each(function ($item) use (&$channel_arr) {
            $channel_arr[$item->id] =$item->title."({$item->country->coun_c_name})";
        });

        $this->success(lang('获取成功'), $channel_arr);
    }

    /**
     * 根据公司获得商户
     *
     * @param $account_id
     * @return array
     */
    public function getAttrShopList ($account_id)
    {
        $map = [
            ['account_id', '=', $account_id],
            ['status', '=', 1],
            ['is_deleted', '=', 0]
        ];
        $data = ShopsModel::mk()->where($map)->select()->column('name','id');
        $this->success(lang('获取成功'), $data);
    }

    /**
     * 根据公司获得仓库
     *
     * @param $account_id
     * @return array
     */
    public function getWarehouseList ($account_id)
    {
        $data = WarehouseModel::mk()->where('account_id',$account_id)->select()->column('warehouse_c_name','id');
        $this->success(lang('获取成功'), $data);
    }


    /**
     * 根据国家获得海外仓库
     *
     * @param $country_id
     * @return array
     */
    public function getWarehouseAbroadList ($country_id)
    {
        $data = WarehouseAbroadModel::mk()->where('country_id',$country_id)->select()->column('warehouse_c_name','id');
        $this->success(lang('获取成功'), $data);
    }

}
